//
//  HostTag.m
//  penzer
//
//  Created by caiyc on 2018/10/11.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "HostTag.h"

@implementation HostTag

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)setArr:(NSArray *)arr{
    _arr = arr;
    CGFloat marginX = 15;
    CGFloat marginY = 10;
    CGFloat height = 28;
    UIButton * markBtn;
    for (int i = 0; i < _arr.count; i++) {
        CGFloat width =  [self calculateString:_arr[i] Width:15] +30;
        UIButton * tagBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        if (!markBtn) {
            tagBtn.frame = CGRectMake(marginX, marginY, width, height);
        }else{
            if (markBtn.frame.origin.x + markBtn.frame.size.width + marginX + width + marginX > screen_width) {
                tagBtn.frame = CGRectMake(marginX, markBtn.frame.origin.y + markBtn.frame.size.height + marginY, width, height);
            }else{
                tagBtn.frame = CGRectMake(markBtn.frame.origin.x + markBtn.frame.size.width + marginX, markBtn.frame.origin.y, width, height);
            }
        }
        [tagBtn setTitle:_arr[i] forState:UIControlStateNormal];
        [tagBtn setTitleEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
        tagBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:15];
        tagBtn.tag = i;
        //
        [tagBtn setTitleColor:RGB(0, 0, 0) forState:UIControlStateNormal];
        tagBtn.backgroundColor = RGB(225, 225, 225);
     //   [self makeCornerRadius:14 borderColor:RGB(180, 180, 180) layer:tagBtn.layer borderWidth:.5];
        markBtn = tagBtn;
        tagBtn.layer.cornerRadius = 5;
        if(i==0){
//            tagBtn.backgroundColor = BUTTON_COLOR;
//            tagBtn.layer.cornerRadius = 14;
        }else{
            
        }
        [tagBtn addTarget:self action:@selector(clickTo:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:markBtn];
    }
    CGRect rect = self.frame;
    rect.size.height = markBtn.frame.origin.y + markBtn.frame.size.height + marginY;
    self.frame = rect;
}
-(void)clickTo:(UIButton *)sender
{
    NSInteger sendTag = sender.tag;
    NSInteger pindex = self.tag;
    sender.selected = !sender.selected;
    if(sender.selected==1){
        sender.backgroundColor = BUTTON_COLOR;
    }else{
        sender.backgroundColor = RGB(225, 225, 225);
    }
   if ([self.delegate respondsToSelector:@selector(handleSelectTag:)]) {
        [self.delegate handleSelectTag:sender.titleLabel.text];
    }
    if ([self.delegate respondsToSelector:@selector(handleSelectTag:andTag:AndPindex:Andselct:)]) {
        [self.delegate handleSelectTag:sender.titleLabel.text andTag:sendTag AndPindex:pindex Andselct:sender.selected];
    }
}



-(void)makeCornerRadius:(CGFloat)radius borderColor:(UIColor *)borderColor layer:(CALayer *)layer borderWidth:(CGFloat)borderWidth
{
    layer.cornerRadius = radius;
    layer.masksToBounds = YES;
    layer.borderColor = borderColor.CGColor;
    layer.borderWidth = borderWidth;
}

-(CGFloat)calculateString:(NSString *)str Width:(NSInteger)font
{
    CGSize size = [str boundingRectWithSize:CGSizeMake(screen_width, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:font]} context:nil].size;
    return size.width;
}

@end
